import { Model, DataTypes, Optional } from 'sequelize';
import sequelize from '@/config/database';

// 定义<%= functionName %>属性接口
export interface <%= ClassName %>Attributes {
<% columns.forEach(function(column) { %>
  <%= column.javaField %>: <%= column.getTypeScriptType() %><% if(!column.isRequiredField() && !column.isPrimaryKey()) { %>?<% } %>;
<% }); %>
}

// 创建时可选的字段
export interface <%= ClassName %>CreationAttributes extends Optional<<%= ClassName %>Attributes, '<%= pkColumn.javaField %>'> {}

/**
 * <%= functionName %>模型类
 * @class <%= ClassName %>
 * @extends {Model<<%= ClassName %>Attributes, <%= ClassName %>CreationAttributes>}
 */
class <%= ClassName %> extends Model<<%= ClassName %>Attributes, <%= ClassName %>CreationAttributes> implements <%= ClassName %>Attributes {
<% columns.forEach(function(column) { %>
  <% if(column.isRequiredField() || column.isPrimaryKey()) { %>
  public <%= column.javaField %>!: <%= column.getTypeScriptType() %>;
  <% } else { %>
  public <%= column.javaField %>?: <%= column.getTypeScriptType() %>;
  <% } %>
<% }); %>

  // 时间戳
  public readonly createTime!: Date;
  public readonly updateTime!: Date;

  // 静态属性，用于常量定义
  public static readonly STATUS = {
    NORMAL: '0',   // 正常状态
    DISABLED: '1', // 停用状态
    DELETED: '2'   // 删除状态
  };
}

<%= ClassName %>.init(
  {
<% columns.forEach(function(column) { %>
    <%= column.javaField %>: {
      type: <%= column.getSequelizeType() %>,
<% if(column.isPrimaryKey()) { %>
      primaryKey: true,
<% if(column.isAutoIncrement()) { %>
      autoIncrement: true,
<% } %>
<% } %>
<% if(!column.isRequiredField() && !column.isPrimaryKey()) { %>
      allowNull: true,
<% } else if(!column.isPrimaryKey()) { %>
      allowNull: false,
<% } %>
<% if(column.columnComment) { %>
      comment: '<%= column.columnComment %>',
<% } %>
      field: '<%= column.columnName %>'
    },
<% }); %>
  },
  {
    sequelize,
    tableName: '<%= tableName %>',
    timestamps: true,
    createdAt: 'createTime',
    updatedAt: 'updateTime',
    hooks: {
      beforeCreate: (<%= className %>: <%= ClassName %>) => {
        if (!<%= className %>.createTime) {
          <%= className %>.createTime = new Date();
        }
      },
      beforeUpdate: (<%= className %>: <%= ClassName %>) => {
        <%= className %>.updateTime = new Date();
      }
    },
    // 定义模型选项
    defaultScope: {
      attributes: { 
        exclude: [] // 默认排除的字段，如需排除某些字段可在此处添加
      }
    }
  }
);

export default <%= ClassName %>; 